package com.zdlyh.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zdlyh.constants.SystemConstants;
import com.zdlyh.domain.ResponseResult;
import com.zdlyh.domain.dto.LinkDto;
import com.zdlyh.domain.entity.Link;
import com.zdlyh.service.LinkService;
import com.zdlyh.mapper.LinkMapper;
import com.zdlyh.utils.BeanCopyUtils;
import com.zdlyh.domain.vo.LinkListVo;
import com.zdlyh.domain.vo.LinkVo;
import com.zdlyh.domain.vo.PageVo;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class LinkServiceImpl extends ServiceImpl<LinkMapper, Link> implements LinkService{
    @Override
    public ResponseResult getAllLink() {
        LambdaQueryWrapper<Link> linkQueryWrapper = new LambdaQueryWrapper();
        linkQueryWrapper.eq(Link::getStatus, SystemConstants.Link_STATUS_NORMAL);
        List<Link> list = baseMapper.selectList(linkQueryWrapper);
        List<LinkListVo> linkListVos = BeanCopyUtils.copyBeanList(list, LinkListVo.class);
        return ResponseResult.okResult(linkListVos);
    }

    @Override
    public ResponseResult getLinkList(Integer pageNum, Integer pageSize, LinkDto linkDto) {
        LambdaQueryWrapper<Link> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.like(StringUtils.hasText(linkDto.getName()),Link::getName,linkDto.getName())
                .eq(StringUtils.hasText(linkDto.getStatus()),Link::getStatus,linkDto.getStatus());
        Page<Link> page = new Page<>(pageNum,pageSize);
        List<Link> linkList = baseMapper.selectPage(page, queryWrapper).getRecords();

        List<LinkVo> linkVos = BeanCopyUtils.copyBeanList(linkList, LinkVo.class);

        PageVo pageVo = new PageVo(linkVos, page.getTotal());
        return ResponseResult.okResult(pageVo);
    }

    @Override
    public ResponseResult addLink(LinkDto linkDto) {
        Link link = BeanCopyUtils.copyBean(linkDto, Link.class);
        baseMapper.insert(link);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult getLinkById(Long id) {
        Link link = baseMapper.selectById(id);
        LinkVo linkVo = BeanCopyUtils.copyBean(link, LinkVo.class);
        return ResponseResult.okResult(linkVo);
    }

    @Override
    public ResponseResult updateLink(LinkDto linkDto) {
        Link link = BeanCopyUtils.copyBean(linkDto, Link.class);
        baseMapper.updateById(link);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult deleteLinkById(Long id) {
        baseMapper.deleteById(id);
        return ResponseResult.okResult();
    }
}




